/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.skin.bezel;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.JTextComponent;

import net.miginfocom.swing.MigLayout;

import org.kommando.core.search.Result;
import org.kommando.skin.bezel.util.SoftClipPainter;
import org.kommando.skin.bezel.util.SoftClipPanel;
import org.kommando.skin.bezel.util.StackLayout;
import org.kommando.ui.EmptyIcon;

/**
 * @author Peter De Bruycker
 */
public class BezelSelectorPanel {

    private boolean selected = false;
    private JLabel iconLabel;
    private JLabel nameLabel;
    private Dimension iconSize;
    private JPanel panel;
    private JScrollPane textScrollPane;
    private Result<?> result;
    private JPanel textPanel;

    private abstract class PanelPainter extends SoftClipPainter {
        @Override
        protected Shape getClip(JComponent component) {
            return new RoundRectangle2D.Double(0, 0, component.getWidth(), component.getHeight(), 25, 25);
        }

        @Override
        protected void doPaint(Graphics2D graphics, JComponent component) {
            graphics.setColor(Color.WHITE);
            graphics.fillRect(component.getX(), component.getY(), component.getWidth(), component.getHeight());
        }

        @Override
        protected void doPaint(Graphics2D graphics, BufferedImage image, int x, int y) {
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha()));
            super.doPaint(graphics, image, x, y);
        }

        protected abstract float getAlpha();
    };

    public BezelSelectorPanel(Dimension iconSize) {
        panel = new SoftClipPanel(new PanelPainter() {
            @Override
            protected float getAlpha() {
                return selected ? 0.5f : 0.1f;
            }
        });
        panel.setLayout(new StackLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel iconAndNamePanel = new JPanel(new MigLayout("align center", "", "[]5[]"));
        iconAndNamePanel.setOpaque(false);
        iconLabel = new JLabel(new EmptyIcon(iconSize), JLabel.CENTER);

        // TODO because label is showing html, there is no elips when there's too much text
        nameLabel = new JLabel(" ", JLabel.CENTER);
        Dimension preferredSize = nameLabel.getPreferredSize();
        nameLabel.setVerticalAlignment(JLabel.TOP);
        nameLabel.setForeground(Color.WHITE);

        iconAndNamePanel.add(iconLabel, "wrap, w " + iconSize.width + ", h " + iconSize.height);
        iconAndNamePanel.add(nameLabel, "w " + iconSize.width + "!, hmax " + preferredSize.height);

        textPanel = new SoftClipPanel(new PanelPainter() {
            @Override
            protected float getAlpha() {
                return 0.7f;
            }
        });
        textPanel.setLayout(new MigLayout());
        textPanel.setOpaque(false);

        textScrollPane = new JScrollPane();
        textScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        textScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        textScrollPane.setOpaque(false);
        textScrollPane.getViewport().setOpaque(false);
        textScrollPane.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));

        textPanel.add(textScrollPane, "w " + iconSize.width);
        textPanel.setVisible(false);

        panel.add(iconAndNamePanel);
        panel.add(textPanel);

        this.iconSize = iconSize;
    }

    public void setResult(Result<?> result) {
        this.result = result;

        iconLabel.setIcon(null);
        nameLabel.setText(" ");

        if (result != null) {
            iconLabel.setIcon(result.getIcon(iconSize));
            nameLabel.setText(result.getHtmlFormattedName());
        }
    }

    public void setTextComponent(JTextComponent textComponent) {
        textComponent.setAutoscrolls(false);
        textComponent.setOpaque(false);
        textScrollPane.setViewportView(textComponent);
    }

    public JComponent getComponent() {
        return panel;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        panel.repaint();
    }

    public void setInTextMode(boolean inTextMode) {
        textPanel.setVisible(inTextMode);
    }

    public Result<?> getResult() {
        return result;
    }
}
